package com.iafme.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iafme.dto.req.StudyReqDTO;
import com.iafme.dto.res.StudyResDTO;
import com.iafme.mapper.StudyMapper;
import com.iafme.model.Study;
import com.iafme.service.StudyService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author  summer
 * @date  2025/1/21 12:20
 * @version 1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StudyServiceImpl extends ServiceImpl<StudyMapper,Study> implements StudyService {

    @Override
    public Page<StudyResDTO> lists(StudyReqDTO studyReqDTO) {
        Page<Study> page=new Page<>(studyReqDTO.getPageNum(), studyReqDTO.getPageSize());
        LambdaQueryWrapper<Study> lambdaQueryWrapper = Wrappers.lambdaQuery(Study.class)
                .eq(Study::getType, studyReqDTO.getType())
                .eq(ObjectUtil.isNotEmpty(studyReqDTO.getHotspot()),Study::getHotspot,studyReqDTO.getHotspot())
                .like(StrUtil.isNotEmpty(studyReqDTO.getName()), Study::getName, studyReqDTO.getName())
                .orderByDesc(Study::getCreateTime);
        page=this.page(page, lambdaQueryWrapper);
        return Convert.convert(new TypeReference<Page<StudyResDTO>>() {},page);
    }
}
